package com.cn.ant.modules.wechat.web;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.wechat.entity.WeixinAccount;
import com.cn.ant.modules.wechat.service.WeixinAccountServiceImpl;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author onlineGenerator
 * @version V1.0
 * @Title: Controller
 * @Description: 微信公众帐号信息
 * @date 2014-05-21 00:53:47
 */
@Controller
@RequestMapping("${adminPath}/weixinAccountController")
public class WeixinAccountController extends BaseController {
    private static final Logger logger = Logger.getLogger(WeixinAccountController.class);

    @Autowired
    private WeixinAccountServiceImpl weixinAccountService;

    @ModelAttribute("weixinAccount")
    public WeixinAccount get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return weixinAccountService.get(id);
        } else {
            return new WeixinAccount();
        }
    }

    @RequiresPermissions("wechat:account:list")
    @RequestMapping(value = {"list", ""})
    public String list(WeixinAccount weixinAccount, Model model, HttpServletRequest request,HttpServletResponse response) {
        Map<String, Object> param = new HashMap<String,Object>();
        Page<WeixinAccount> page = weixinAccountService.query(new Page<WeixinAccount>(request, response), param);
        model.addAttribute("page",page);
        return "modules/wechat/account/weixinAccountList";
    }

    @RequiresPermissions("wechat:account:edit")
    @RequestMapping(value = "form")
    public String form(WeixinAccount weixinAccount, Model model, HttpServletRequest request) {
        return "modules/wechat/account/wexinAccountForm";
    }

    @RequiresPermissions("wechat:account:edit")
    @RequestMapping(value = "save")
    @ResponseBody
    public AjaxJson save(WeixinAccount weixinAccount, Model model, HttpServletRequest request) {
        AjaxJson jo = new AjaxJson();
        try {
            weixinAccountService.saveOrUpdate(weixinAccount);
            jo.setSuccess(true);
            jo.setMsg("添加公众号成功");
        } catch (Exception e) {
            e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("添加公众号失败，失败原因：" + e.getMessage());
        }
        return jo;
    }

    @RequiresPermissions("wechat:account:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public AjaxJson delete(@RequestParam(required = true) String id) {
        AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(id)) {
            weixinAccountService.delete(id);
            jo.setMsg("删除公众号成功");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
    }
}
